${basePackage};

import java.util.ArrayList;
import java.util.List;

import ldh.serialize.LazyMethod;
import ldh.serialize.SerializedItem;
import ldh.serialize.util.SerializedUtil;
<#list imports as import>
import ${import};
</#list>

public class ${beanName}Serialize  {

	private static List<SerializedItem<${beanName}, ?>> data = new ArrayList<SerializedItem<${beanName}, ?>>();
	
	static{
		<#list classInfo.getDatas() as classData>
		<#if util.isArray(classData)>
		data.add(new SerializedItem(new ${util.firstUpper(classData.property)}(), ${util.getSerializedName(classData)}));
		<#elseif util.isIterable(classData)>
		data.add(new SerializedItem(new ${util.firstUpper(classData.property)}(), ${util.getSerializedName(classData)}));
		<#elseif util.isMap(classData)>
		data.add(new SerializedItem(new ${util.firstUpper(classData.property)}(), ${util.getSerializedName(classData)}));
		<#else>
		data.add(new SerializedItem<${beanName}, ${classData.getJavaType()}>(new ${util.firstUpper(classData.property)}(), ${util.getSerializedName(classData)}));
		</#if>
		</#list>
	}
	
	public static byte[] toBytes(${beanName} ${util.firstLower(beanName)}) {
		return SerializedUtil.toBytes(data, ${util.firstLower(beanName)});
	}
	
	public static String toJson(${beanName} ${util.firstLower(beanName)}) {
		return SerializedUtil.toJson(data, ${util.firstLower(beanName)});
	}
	
	public static ${beanName} toObject(byte[] bytes) {
		${beanName} ${util.firstLower(beanName)} = new ${beanName}();
		return SerializedUtil.toObject(data, ${util.firstLower(beanName)}, bytes);
	}
	
	<#list classInfo.getDatas() as classData>
	<#if util.isArray(classData)>
	private static class ${util.firstUpper(classData.property)} implements LazyMethod<${beanName}, Object[]> {
		
		@Override
		public Object[] getValue(${beanName} ${util.firstLower(beanName)}) {
			return ${util.firstLower(beanName)}.get${util.firstUpper(classData.property)}();
		}

		@Override
		public void setValue(${beanName} ${util.firstLower(beanName)}, Object[] t) {
			${util.firstUpper(classData.getJavaType())} tt = new ${util.firstUpper(classData.getSimpleJavaType())}[t.length];
			int i = 0;
			for (Object o : t) {
				tt[i++] = (${util.firstUpper(classData.getSimpleJavaType())}) o;
			}
			${util.firstLower(beanName)}.set${util.firstUpper(classData.property)}(tt);
		}

		@Override
		public Class getClassType() {
			return ${util.firstUpper(classData.getSimpleJavaType())}.class;
		}
		
		public String getPropertyName() {
			return "${util.firstLower(classData.property)}";
		}
		
	}
	<#else>
	private static class ${util.firstUpper(classData.property)} implements LazyMethod<${beanName}, ${util.firstUpper(classData.getJavaType())}> {
		
		@Override
		public ${util.firstUpper(classData.getJavaType())} getValue(${beanName} ${util.firstLower(beanName)}) {
			return ${util.firstLower(beanName)}.get${util.firstUpper(classData.property)}();
		}

		@Override
		public void setValue(${beanName} ${util.firstLower(beanName)}, ${util.firstUpper(classData.getJavaType())} t) {
			${util.firstLower(beanName)}.set${util.firstUpper(classData.property)}(t);
		}

		@Override
		public Class getClassType() {
			return ${util.firstUpper(classData.getSimpleJavaType())}.class;
		}
		
		public String getPropertyName() {
			return "${util.firstLower(classData.property)}";
		}
		
	}
	</#if>
	
	</#list>

}
